package sisi.invoices;

import java.util.List;

import javax.persistence.EntityManager;

public class SuppinvoiceController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Suppinvoice[] get(Integer numOrder, Integer idSupplier) {
		List<Suppinvoice> lista = getList(numOrder, idSupplier);
			return lista.toArray(new Suppinvoice[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Suppinvoice> getList(Integer numOrder, Integer idSupplier) {

		String cWhere = "";
		cWhere = " WHERE c.order="+numOrder+" AND c.suppid="+idSupplier;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoice as c " + cWhere 
					+ " order by c.order ");

			List<Suppinvoice> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Suppinvoice> getList(Integer numOrder) {

		String cWhere = "";
		cWhere = " WHERE c.order="+numOrder;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoice as c " + cWhere 
					+ " order by c.order ");

			List<Suppinvoice> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Suppinvoice getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Suppinvoice suppinvoicex = em.find(Suppinvoice.class, id);
			return suppinvoicex;
		} finally {
			em.close();
		}
	}
	
	
	public int getCountEqualsEnqNumber(Integer numorder) {

		EntityManager em = getEntityManager();
		try {
			String cString = "select count(c) from Suppinvoice c ";
			
				cString += " WHERE ";			
				cString += " c.order = "+numorder; 
						
			javax.persistence.Query q = em
					.createQuery(cString);
						
			Long result = ((Long) q.getSingleResult());
			int quantirecords = result.intValue();
			return quantirecords;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Suppinvoice add(Suppinvoice suppinvoice) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(suppinvoice);
			em.getTransaction().commit();
			suppinvoice = refresh(suppinvoice);
		} finally {
			em.close();
			return suppinvoice;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Suppinvoice suppinvoice) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Suppinvoice suppinvoicex = em.find(Suppinvoice.class, suppinvoice.getRecnum());
			suppinvoicex.setType(suppinvoice.getType());
			suppinvoicex.setOrder(suppinvoice.getOrder());
			suppinvoicex.setDeliverydt(suppinvoice.getDeliverydt());
			suppinvoicex.setPorttoref(suppinvoice.getPorttoref());
			suppinvoicex.setArt15(suppinvoice.getArt15());
			suppinvoicex.setSuppid(suppinvoice.getSuppid());
			suppinvoicex.setCurr(suppinvoice.getCurr());
			suppinvoicex.setStat(suppinvoice.getStat());
			suppinvoicex.setPaymode(suppinvoice.getPaymode());
			suppinvoicex.setVatyesno(suppinvoice.getVatyesno());
			suppinvoicex.setRecnum(suppinvoice.getRecnum());
			suppinvoicex.setBankdoc(suppinvoice.getBankdoc());
			suppinvoicex.setBankdtdoc(suppinvoice.getBankdtdoc());
			suppinvoicex.setCcType(suppinvoice.getCcType());
			suppinvoicex.setChange(suppinvoice.getChange());
			suppinvoicex.setClipobject(suppinvoice.getClipobject());
			suppinvoicex.setCreditterms(suppinvoice.getCreditterms());
			suppinvoicex.setCustid(suppinvoice.getCustid());
			suppinvoicex.setCustname(suppinvoice.getCustname());
			suppinvoicex.setDeleted(suppinvoice.getDeleted());
			suppinvoicex.setDn(suppinvoice.getDn());
			suppinvoicex.setDtUp(suppinvoice.getDtUp());
			suppinvoicex.setDuedate(suppinvoice.getDuedate());
			suppinvoicex.setExchangerate(suppinvoice.getExchangerate());
			suppinvoicex.setInitial(suppinvoice.getInitial());
			suppinvoicex.setInvoicedt(suppinvoice.getInvoicedt());
			suppinvoicex.setInvoicedtreg(suppinvoice.getInvoicedtreg());
			suppinvoicex.setMemento(suppinvoice.getMemento());
			suppinvoicex.setNfysNote(suppinvoice.getNfysNote());
			suppinvoicex.setPaid(suppinvoice.getPaid());
			suppinvoicex.setPaidamount(suppinvoice.getPaidamount());
			suppinvoicex.setPayddt(suppinvoice.getPayddt());
			suppinvoicex.setPortidpass(suppinvoice.getPortidpass());
			suppinvoicex.setRecbook(suppinvoice.getRecbook());
			suppinvoicex.setSuppname(suppinvoice.getSuppname());
			suppinvoicex.setSuppnuminvoice(suppinvoice.getSuppnuminvoice());
			suppinvoicex.setSuppval(suppinvoice.getSuppval());
			suppinvoicex.setTdoc(suppinvoice.getTdoc());
			suppinvoicex.setTotalinvoice(suppinvoice.getTotalinvoice());
			suppinvoicex.setTottaxed(suppinvoice.getTottaxed());
			suppinvoicex.setTotvat(suppinvoice.getTotvat());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Suppinvoice refresh(Suppinvoice suppinvoice) {

		Suppinvoice suppinvoicex = suppinvoice;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			suppinvoicex = em.find(Suppinvoice.class, suppinvoice.getRecnum());
			em.flush();
			em.refresh(suppinvoicex);
		} finally {
			em.close();
			return suppinvoicex;
		}
}

@SuppressWarnings("finally")
public boolean remove(Suppinvoice suppinvoice) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Suppinvoice suppinvoicex = em.find(Suppinvoice.class, suppinvoice.getRecnum());
			em.remove(suppinvoicex);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//

